package cn.iocoder.yudao.module.member.controller.admin.newsStudyType;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.controller.admin.newsStudyType.vo.*;
import cn.iocoder.yudao.module.member.convert.newsStudyType.NewsStudyTypeConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.newsStudyType.NewsStudyTypeDO;
import cn.iocoder.yudao.module.member.service.newsStudyType.NewsStudyTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 -运营端- 新闻栏目(成果类型)")
@RestController
@RequestMapping("/member/newsStudyType")
@Validated
public class NewsStudyTypeController {

    @Resource
    private NewsStudyTypeService newsStudyTypeService;

    @PostMapping("/create")
    @ApiOperation("创建新闻栏目(成果类型)")
    @TenantIgnore
    public CommonResult<Long> createNewsStudyType(@Valid @RequestBody NewsStudyTypeCreateReqVO createReqVO) {
        if("1".equals(createReqVO.getStatus())){
            createReqVO.setReleaseTime(new Date());
        }
        createReqVO.setWeight(0);
        return success(newsStudyTypeService.createNewsStudyType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新新闻栏目(成果类型)")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-type:update')")
    public CommonResult<Boolean> updateNewsStudyType(@Valid @RequestBody NewsStudyTypeUpdateReqVO updateReqVO) {
        if("1".equals(updateReqVO.getStatus())){
            updateReqVO.setReleaseTime(new Date());
        }
        newsStudyTypeService.updateNewsStudyType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @TenantIgnore
    @ApiOperation("删除新闻栏目(成果类型)")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    //@PreAuthorize("@ss.hasPermission('member:news-study-type:delete')")
    public CommonResult<Boolean> deleteNewsStudyType(@RequestParam("id") Long id) {
        newsStudyTypeService.deleteNewsStudyType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得新闻栏目(成果类型)")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    //@PreAuthorize("@ss.hasPermission('member:news-study-type:query')")
    public CommonResult<NewsStudyTypeRespVO> getNewsStudyType(@RequestParam("id") Long id) {
        NewsStudyTypeDO newsStudyType = newsStudyTypeService.getNewsStudyType(id);
        return success(NewsStudyTypeConvert.INSTANCE.convert(newsStudyType));
    }

    @GetMapping("/list")
    @ApiOperation("获得新闻栏目(成果类型)列表")
    @TenantIgnore
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    //@PreAuthorize("@ss.hasPermission('member:news-study-type:query')")
    public CommonResult<List<NewsStudyTypeRespVO>> getNewsStudyTypeList(@RequestParam("ids") Collection<Long> ids) {
        List<NewsStudyTypeDO> list = newsStudyTypeService.getNewsStudyTypeList(ids);
        return success(NewsStudyTypeConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("获得新闻栏目(成果类型)分页")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-type:query')")
    public CommonResult<PageResult<NewsStudyTypeRespVO>> getNewsStudyTypePage( @RequestBody NewsStudyTypePageReqVO pageVO) {
        if(null==pageVO.getDeleted()){
            pageVO.setDeleted(0);
        }
        if(  null==pageVO.getSortField()){
            List<SortingField> list =new ArrayList<>();
            SortingField sf=new SortingField();
            sf.setField("weight");
            sf.setOrder("desc");
            list.add(sf);
            sf=new SortingField();
            sf.setField("updateTime");
            sf.setOrder("asc");
            list.add(sf);
            pageVO.setSortingFields(list);
        }
        PageResult<NewsStudyTypeDO> pageResult = newsStudyTypeService.getNewsStudyTypeList(pageVO);
        return success(NewsStudyTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出新闻栏目(成果类型) Excel")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-type:export')")
    @OperateLog(type = EXPORT)
    public void exportNewsStudyTypeExcel(@Valid NewsStudyTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<NewsStudyTypeDO> list = newsStudyTypeService.getNewsStudyTypeList(exportReqVO);
        // 导出 Excel
        List<NewsStudyTypeExcelVO> datas = NewsStudyTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "新闻栏目(成果类型).xls", "数据", NewsStudyTypeExcelVO.class, datas);
    }
    @PutMapping("/changeStatus")
    @ApiOperation("上下架")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-type:update')")
    public CommonResult<Boolean> changeStatus(@RequestBody NewsStudyTypeUpdateReqVO updateReqVO) {
        if(null==updateReqVO.getId()){
           return error("ID不能为空");
        }
        if(StringUtils.isBlank(updateReqVO.getStatus())){
            return error("状态不能为空");
        }
        NewsStudyTypeDO newsStudyTypeDO=new NewsStudyTypeDO();
        newsStudyTypeDO.setId(updateReqVO.getId());
        newsStudyTypeDO.setStatus(updateReqVO.getStatus());
        newsStudyTypeService.updateNewsStudyTypeStatus(newsStudyTypeDO);
        return success(true);
    }

    @PostMapping("/recoveryPage")
    @ApiOperation("回收站")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-type:query')")
    public CommonResult<PageResult<NewsStudyTypeRespVO>> recoveryPage(@Valid @RequestBody NewsStudyTypePageReqVO pageVO) {
        pageVO.setDeleted(1);
        if(null==pageVO.getSortField()){
            SortingField sf=new SortingField();
            sf.setField("createTime");
            sf.setOrder("desc");
            pageVO.setSortField(sf);
        }
        PageResult<NewsStudyTypeDO> pageResult = newsStudyTypeService.getNewsStudyTypeList(pageVO);
        return success(NewsStudyTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @PutMapping("/updateBatch")
    @ApiOperation("回收站还原")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-type:update')")
    public CommonResult<Boolean> updateBatch(@Valid  @RequestBody NewsStudyTypeIdReqVO updateReqVO ) {
        newsStudyTypeService.updateBatch(updateReqVO.getIds());
        return success(true);
    }

    @PutMapping("/updateNewsStudyTypeSort")
    @ApiOperation("新闻栏目(成果类型)排序")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:delete')")
    public CommonResult<Boolean> updateNewsStudyTypeSort(@Valid @RequestBody NewsStudyTypeIdReqVO updateReqVO ) {
        newsStudyTypeService.updateNewsStudyTypeSort(updateReqVO.getIds());
        return success(true);
    }
}
